package com.example.android.network;

import java.util.HashMap;

import com.example.android.network.action.Action;

public class ActionFactory
{
	private static final String classPath = "com.example.android.network.action.";
	private static HashMap<InPacketType, Action> actions = new HashMap<InPacketType, Action>();

	static
	{
		for (InPacketType packetType : InPacketType.values())
		{
			try
			{
				Class<?> actionClass = Class.forName(classPath
						+ packetType.toString());
				actions.put(packetType, (Action) actionClass.newInstance());
			} catch (Exception e)
			{
				e.printStackTrace();
			}
		}
	}

	public static void execute(String packet)
	{
		System.err.println(packet);
		PacketParser packetParser = new PacketParser(packet);
		Action action = actions.get(packetParser.getPacketType());
		action.execute(packetParser.getValues());
	}
}
